package com.hundsun.yr.universal.library.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

import com.hundsun.yr.universal.library.R;
import com.hundsun.yr.universal.library.common.DensityUtils;

/**
 * @Title: HSLabelTextView.java
 * @Description: 标签文字爱
 * @date 2015-12-23 下午3:42:03
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 * @version V1.0
 */
public class HSLabelTextView extends TextView {
	/**默认边框圆角**/
	private final static int DEFAULT_CONNERS=8;
	/**默认边框宽度**/
	private final static int DEFAULT_BORDEWIDTH=2; 
	/**边框画笔**/
	private Paint paintBorder;
	/**边框宽度**/
	private int borderWidth ;
	/**边框颜色**/
	private int borderColor;
	/**边框圆角度**/
	private int conners;
	/**变宽内颜色**/
	private int backColor;
	public HSLabelTextView(Context context) {
		this(context, null);
	}

	public HSLabelTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context,attrs);
	}

	public HSLabelTextView(Context context, AttributeSet attrs) {
		super(context, attrs,0);
		init(context,attrs);
	}

	private void init(Context context, AttributeSet attrs)  {
		paintBorder = new Paint();
		paintBorder.setFlags(Paint.ANTI_ALIAS_FLAG);
		paintBorder.setAntiAlias(true);
		paintBorder.setStyle(Style.STROKE);      
		setGravity(Gravity.CENTER);
		setIncludeFontPadding(false);
		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.HSLabelTextView);
		if (a.hasValue(R.styleable.HSLabelTextView_hs_ltv_borderColor)) {
			borderColor = a.getColor(R.styleable.HSLabelTextView_hs_ltv_borderColor,getResources().getColor(R.color.transparent));
		}else{
			borderColor = getTextColors().getDefaultColor();
		}
		paintBorder.setColor(borderColor);

		if (a.hasValue(R.styleable.HSLabelTextView_hs_ltv_backColor)) {
			backColor = a.getColor(R.styleable.HSLabelTextView_hs_ltv_backColor,getResources().getColor(R.color.transparent));
		}else{
			backColor = Color.TRANSPARENT;
		}
		
		if (a.hasValue(R.styleable.HSLabelTextView_hs_ltv_borderWidth)) {
			borderWidth = (int) a.getDimension(R.styleable.HSLabelTextView_hs_ltv_borderWidth,DensityUtils.dp2px(context, 1));
		}else{
			borderWidth=DEFAULT_BORDEWIDTH;
		}
		paintBorder.setStrokeWidth((float) borderWidth);   
		if (a.hasValue(R.styleable.HSLabelTextView_hs_ltv_corners)) {
			conners = (int) a.getDimension(R.styleable.HSLabelTextView_hs_ltv_corners,DensityUtils.dp2px(context, 4));
		}else{
			conners=DEFAULT_CONNERS;
		}
        
		a.recycle();
//		
		StrokeGradientDrawable drawable=createDrawable(conners, borderColor,borderWidth);
		setBackgroundCompat(drawable.getGradientDrawable());
	}
	/**
	 * 有锯齿
	 */
//	@SuppressLint("DrawAllocation") @Override
//	protected void onDraw(Canvas canvas) {
//		super.onDraw(canvas);
//		RectF rect=new RectF();
//		rect.top=0;
//		rect.left=0;
//		rect.right= getWidth();
//		rect.bottom=getHeight();
//		//绘制边框
//		canvas.drawRoundRect(rect, conners, conners, paintBorder);
//	}
	
	
	/**
	 * 含边框
	 * @param cornerRadius
	 * @param strokeColor
	 * @param strokeWidth
	 * @return
	 */
	private StrokeGradientDrawable createDrawable(int cornerRadius,int strokeColor, int strokeWidth) {
		final StrokeGradientDrawable drawable = new StrokeGradientDrawable(new GradientDrawable());
		drawable.getGradientDrawable().setShape(GradientDrawable.RECTANGLE);
		drawable.setStrokeColor(strokeColor);
		drawable.setStrokeWidth(strokeWidth);
		drawable.setCornerRadius(cornerRadius);
		drawable.setColor(backColor);
		return drawable;
	}
	
	@SuppressLint("NewApi") @SuppressWarnings("deprecation")
	private void setBackgroundCompat(@Nullable Drawable drawable) {
		if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
			setBackgroundDrawable(drawable);
		} else {
			setBackground(drawable);
		}
	}

	public void setBorderColor(int color){
		borderColor = color;
		StrokeGradientDrawable drawable=createDrawable(conners, borderColor,borderWidth);
		setBackgroundCompat(drawable.getGradientDrawable());
	}

	public void setBackColor(int color){
		backColor = color;
		StrokeGradientDrawable drawable=createDrawable(conners, borderColor,borderWidth);
		setBackgroundCompat(drawable.getGradientDrawable());
	}
	
}